package com.example.demomybatis.service.impl;

import cn.hutool.core.lang.Pair;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demomybatis.entity.Person;
import com.example.demomybatis.mapper.PersonMapper;
import com.example.demomybatis.service.IPersonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhaoheng
 * @since 2023-07-07
 */
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements IPersonService {
    @Resource
    PersonMapper personMapper;

    @Override
    public boolean updateBirthday(String name, String localDate) {
        LambdaUpdateWrapper<Person> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(Person::getBirthday, localDate).eq(Person::getName, name);
        return this.update(lambdaUpdateWrapper);
    }

    @Override
    public List<Person> getByNames(List<String> names) {
        LambdaUpdateWrapper<Person> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(Person::getName, names);
        return this.list(lambdaUpdateWrapper);
    }

    @Override
    public List<Person> queryAllPerson() {
        return personMapper.queryAllPerson();
    }

    @Override
    public List<Pair<String, Integer>> queryId() {
        return personMapper.queryId();
    }

    @Override
    public Page<Person> queryPage(int pageNum, int pageSize) {
        Page<Person> page = new Page<>(pageNum, pageSize);
        Page<Person> personPage = personMapper.selectPage(page, null);
        return personPage;
    }
}
